
function launchBTServer() {
    //Global variable PORT is what we will use for now
    dump('\tlaunchBTServer\n');
    port=PORT;
//    RUNNING=true;//for debuging should haver server running
    if (!RUNNING && (torrent_keys.length>0)) {
        RUNNING=true;
        bt_server=getExtensionDir();
        bt_server.append('weblaunchmany');
        bt_server.append('weblaunchmany.py');
        // create an nsIProcess process is a global var
        process = Components.classes["@mozilla.org/process/util;1"]
                                       .createInstance(Components.interfaces.nsIProcess);
        args=[port];
        process.init(bt_server);
        process.run(false,args,args.length);
    }
    if (torrent_keys.length>0) {
        setTimeout('sendTorentInfo();',1000);
    }
    else { alert('There are no torrents in the queue'); }
}


function sendTorentInfo() { 
/*
 * This function takes the torrent_keys global var and
 * feeds them to the torrent server, takes the resulting hash
 * keys from the server and stores them in the torrents global
 * variable
 */
    for (var i=0;i<torrent_keys.length;i++) {
        get_url="http://localhost:"+PORT+"/torrent_file/?torrent="+encodeURIComponent(torrent_keys[i]['torrent_file'])+"&save_path="+encodeURIComponent(torrent_keys[i]['dl_dest']);
        var req = new XMLHttpRequest();
        req.open('GET', get_url, false); 
        req.send(null);
        if(req.status == 200)
            tor=req.responseText.parseJSON();
//            torrents[tor['id']]=torrent_keys[i];
            statuses['running'][tor['id']]=torrent_keys[i];
    }
    torrent_keys=[];
    var req = new XMLHttpRequest();
    req.open('GET',"http://localhost:"+PORT+"/start",false);
    req.send(null);
    if ((req.responseText=='{"message":"All Torrents Started"}') || 
        (req.responseText=='{"message":"torrents started already"}')) {
        dump('Torrent machine started\n');
        return true;
    }
    else {
        dump(req.responseText);dump('\n');
        alert('Error starting Torrent Machine\n');
        return false;
    }
}
